/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-10-20     RT-Thread    first version
 */

#include <rtthread.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include "bsp/bsp_converter.h"
#include "bsp/bsp_spi.h"
#include "bsp/bsp_flash.h"
#include "bsp/bsp_can.h"
#include "bsp/bsp_crc.h"
#include "bsp/bsp_uart2.h"
#include "bsp/bsp_uart6.h"


#include "HJComm/HJMasterDevice.h"
#include "HJComm/HJRfCardDevice.h"

#include "app/app_io.h"
#include "app/app_comm.h"

#define NODE_ID         0
#define SOFT_VERSION    "1.0.0.1"




/*
 * TODO:WJB
  *  启动在线升级 OTA
 * 1.启用ota.c中的设置中断向量
 * 2.rtboot_f4.c中启用boot
 * 3.linkscripts中取消link.lds 改为link_ota.lds
 *
 *
  * 需要核对W25Q128的CS引脚
 *
 * */

/*
 USART1 -> 触摸屏
 USART2 -> 肘托交换板
 USART3 -> 工控机
 USART4 -> 调试串口
 */

char* json_req = "{"
    "\"cmd\": 1,"
    "\"fan\": 1,"
    "\"rgbs\":[{"
        "\"style\": 2,"
        "\"freq\": 2 "
    "},"
    "{"
        "\"style\": 2,"
        "\"freq\": 2 "
    "}],"
    "\"nfc\":\"nfc001type002\""
"}";

char json_rep[1024] = {0};

extern "C" int main(void)
{
    int count = 1;

    bsp_spi_init();

    bsp_flash_init();

    bsp_uart2_init();

    bsp_uart6_init();

//    bsp_can_init();
//    bsp_can_filter_init(NODE_ID);

    bsp_crc_init();

    app_io_init(&gpio_map);

    RfCardrev_thread_init();

    LOG_D("Hello RT-Thread!");
    LOG_D("Ver %s", SOFT_VERSION);

    app_comm_process(json_req, json_rep, sizeof(json_rep));
    bsp_uart6_tx_transmit((uint8_t *) json_rep,  sizeof(json_rep));


    while (count++)
    {
        HJDeiviceFrameHandleWrap(NODE_ID, (uint8_t*)SOFT_VERSION);
    }

    return RT_EOK;
}
